﻿using GrainManage.Entities.District;
using System;

namespace GrainManage.Server.Business
{
    public class DistrictBusiness : BaseBusiness, GrainManage.Interface.IDistrict
    {
        //************************************************POST************************************************
        public OutputGetByID GetByID(InputGetByID input)
        {
            var result = new OutputGetByID();
            try
            {
                var service = ObjectFactory.DistrictService;
                var table = service.Get(input.DistrictID);
                if (table != null)
                {
                    result.District = table;
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputSearch Search(InputSearch input)
        {
            var result = new OutputSearch();
            try
            {
                var service = ObjectFactory.DistrictService;
                result.Districts = service.Search(input.DistrictName, input, result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputGetFather GetFather(InputGetFather input)
        {
            var result = new OutputGetFather();
            try
            {
                var service = ObjectFactory.DistrictService;
                result.District = service.GetFather(input.DistrictID);
                if (result.District == null)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }
            return result;
        }

        public OutputGetChild GetChild(InputGetChild input)
        {
            var result = new OutputGetChild();
            try
            {
                var service = ObjectFactory.DistrictService;
                result.Districts = service.GetChild(input.DistrictID, input, result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputGetUpwardDistrict GetUpwardDistrict(InputGetUpwardDistrict input)
        {
            var result = new OutputGetUpwardDistrict();
            try
            {
                var service = ObjectFactory.DistrictService;
                result.Districts = service.GetUpwardDistrict(input.Id, input.Level, result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputGetDownwardDistrict GetDownwardDistrict(InputGetDownwardDistrict input)
        {
            var result = new OutputGetDownwardDistrict();
            try
            {
                var service = ObjectFactory.DistrictService;
                result.Districts = service.GetDownwardDistrict(input.Id, input.Level, input, result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }
    }
}
